package net.thxjj.adminServer.controller;

import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IMobileUserService;
import net.thxjj.adminServer.service.IUserApiService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.sitermodel.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/mobileUser")
public class MobileUserController extends BaseController {

    @Autowired
    private IUserApiService iUserApiService;

    @Autowired
    private IMobileUserService iMobileUserService;


    /**
     * 获取手机终端用户列表
     * @return
     */
    @RequestMapping(value="/mobileUserList",method= RequestMethod.GET)
    @ResponseBody
    public Object mobileUserList(){

        String username = this.getParameterMap().getString("username");

        String token = this.getRequest().getHeader("token");
        User userinfo = iUserApiService.getUserInfoByToken(token);

        String pid = "";
        if(!"admin".equals(userinfo.getUsername())){
            pid = userinfo.getPid();
        }

        return ResponseModel.getModel(ResultEnum.SUCCESS,iMobileUserService.getUserList(pid,username));

    }


    /**
     * 获取手机终端用户的设备列表
     * @return
     */
    @RequestMapping(value="/deviceList",method= RequestMethod.GET)
    @ResponseBody
    public Object deviceList(){


        return null;

    }

    /**
     * 解绑手机终端用户和相应设备
     * @return
     */
    @RequestMapping(value="/unbindDevice",method= RequestMethod.POST)
    @ResponseBody
    public Object unbindDevice(@RequestBody JSONObject jsonObject){


        return null;

    }


    /**
     * 获取手机终端用户统计数据
     * @return
     */
    @RequestMapping(value="/staticsUser",method= RequestMethod.GET)
    @ResponseBody
    public Object staticsUser(){

        String token = this.getRequest().getHeader("token");
        User userinfo = iUserApiService.getUserInfoByToken(token);

        String pid = "";
        if(!"admin".equals(userinfo.getUsername())){
            pid = userinfo.getPid();
        }

        long totalUser = iMobileUserService.getUserCounts(pid);

        long todayTotalUser = iMobileUserService.getUserCountsByTime(pid);


        List<List<Object>> lists = iMobileUserService.UserTrendsOfLastMonth(pid);

        ParameterMap map = new ParameterMap();
        map.put("total",totalUser);
        map.put("today",todayTotalUser);
        map.put("trend",lists);

        return ResponseModel.getModel(ResultEnum.SUCCESS,map);

    }

}
